package instance

import (
	"sync"

	"gitee.com/linxing_3/sr-core/yerr"
)

var _defaultSet = &Set{
	storage: sync.Map{},
}

type Set struct {
	storage sync.Map
}

func (s *Set) AddInstance(key any, value interface{}) error {
	if _, ok := s.storage.Load(key); ok {
		return yerr.ErrDataRepeat.Errorf("%T", key)
	}

	s.storage.Store(key, value)
	return nil
}

func (s *Set) GetInstance(key any) (interface{}, error) {
	if value, ok := s.storage.Load(key); ok {
		return value, nil
	}

	return nil, yerr.ErrDataNotfound.Errorf("%T", key)
}
